home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 January / macformat46.iso / Shareware Plus / Developers / Library / Grant's CGI Framework / Grant's CGI Framework / grantscgi / Startup / Startup.c next >
Encoding:
C/C++ Source or Header  |  1996-09-23  |  10.2 KB  |  469 lines

  1. /*****
  2.  *
  3.  *    Startup.c
  4.  *
  5.  *    If you want to exclude CGI or ListSTAR support, remove the appropriate lines
  6.  *    from the 'StartupApplication' function.
  7.  *
  8.  *    This is a support file for "Grant's CGI Framework".
  9.  *    Please see the license agreement that accompanies the distribution package
  10.  *    for licensing details.
  11.  *
  12.  *    Copyright ©1995,1996 by Grant Neufeld
  13.  *    grant@acm.com
  14.  *    http://arpp.carleton.ca/cgi/framework/
  15.  *
  16.  *****/
  17.  
  18. #include "MyConfiguration.h"
  19. #if !(kCompilingForWSAPI)
  20.  
  21. #include <Threads.h>
  22. #include <Traps.h>
  23. #if kCompilingForWSAPI
  24. #include <WSAPI.h>
  25. #endif
  26.  
  27. #include "constants.h"
  28. #include "globals.h"
  29.  
  30. #include "AboutBox.h"
  31. #include "AEFunc.h"
  32. #include "AEHandlers.h"
  33. #include "CGI.h"
  34. #include "CustomHandlers.h"
  35. #include "ErrorUtil.h"
  36. #include "EventUtil.h"
  37. #include "ListSTAR.h"
  38. #include "LogUtil.h"
  39. #include "MemoryUtil.h"
  40. #include "MenuFunc.h"
  41. #include "PrefUtil.h"
  42. #include "ProcessUtil.h"
  43. #include "SplashScreen.h"
  44. #include "StringUtil.h"
  45. #include "Version.h"
  46.  
  47. #include "Startup.h"
  48.  
  49.  
  50. /***  LOCAL CONSTANTS ***/
  51.  
  52. #define    TrapMask        0x0800
  53. #define kGestaltMask    1L
  54.  
  55. /* window info */
  56. #define    kScreenBorder    4    /* pixels */
  57. #define    kIconSize        32
  58.  
  59.  
  60. /***  LOCAL PROTOTYPES ***/
  61.  
  62. static    void        startupErrors            ( void );
  63. static    void        startupGestalt            ( void );
  64. static    void        startupEvent            ( void );
  65. static    void        startupAE                ( void );
  66. #if kCompileWithForeground
  67. static    void        startupWindows            ( void );
  68. static    void        startupQuickDraw        ( void );
  69. #if kCompileWithDragNDrop
  70. static    void        startupDrag                ( void );
  71. #endif
  72.  
  73. #else
  74.     /* make the interface function prototypes null */
  75.     #define startupMenus()    
  76.     #define startupWindows()    
  77.     #define startupQuickDraw()    
  78. #endif    /* kCompileWithForeground */
  79.     
  80. #if kCompileWithQuitOnLongIdle
  81. static    void        startupIdleQuit            ( void );
  82. #endif
  83.  
  84. static    Boolean        startupTrapAvailable    ( unsigned long );
  85. static    TrapType    startupTrapType            ( unsigned long );
  86.  
  87.  
  88. /***  FUNCTIONS  ***/
  89.  
  90. void
  91. StartupApplication ( void )
  92. {
  93.     OSErr    theErr;
  94.     
  95.     /* moved to ProcessStartup() */
  96. //    gSleepTicks        = kSleepTicks;    
  97. //    gFrontProcess    = ProcessCurrentIsFront ();
  98. //    #if kCompileWithProcessFileSpec
  99. //    ProcessGetMyFSSpec ( &gProcessFSSpec );
  100. //    #endif
  101.     
  102.     gAppFileRef = CurResFile ();
  103.     
  104.     PrefStartup ();    /* should be before almost everything else */
  105.     
  106.     ProcessStartup        ();
  107.     startupErrors        ();
  108.     
  109.     VersionGetShort        ( 1, gVersionStr );    /* must be before splash screen */
  110.     
  111.     LogStartup            ();    /* set up the log file */
  112.     
  113.     SplashScreenCreate    ();    /* put up splash screen */
  114.  
  115.     startupGestalt        ();    /* must be before all Manager inits except toolbox and memory */
  116.     startupEvent        ();
  117.     startupAE            ();
  118.     MenuStartup            ();
  119.     ThreadStartup        ();
  120.     startupWindows        ();    /* must be after startupMenus */
  121.     startupQuickDraw    ();
  122.     #if kCompileWithDragNDrop
  123.     startupDrag            ();
  124.     #endif
  125.     AboutBoxInit        ();
  126.     
  127.     #if kCompileWithQuitOnLongIdle
  128.     startupIdleQuit        ();
  129.     #endif
  130.     
  131.     theErr = InitCGIUtil ();    /* CGI Specific */
  132.     if ( theErr != noErr )
  133.     {
  134.         ErrorStartup ( kerrStartupCGI );
  135.     }
  136.     
  137.     theErr = InitListSTARUtil ();    /* ListSTAR Specific */
  138.     if ( theErr != noErr )
  139.     {
  140.         ErrorStartup ( kerrStartupListSTAR );
  141.     }
  142.     
  143.     /* • the result of this call should be checked to see whether
  144.         initialization was successful */
  145.     theErr = CustomStartup ();
  146.     if ( theErr != noErr )
  147.     {
  148.         ErrorStartup ( kerrStartupCustomInit );
  149.     }
  150.     
  151.     SplashScreenDispose ();    /* remove splash screen */
  152. } /* StartupApplication */
  153.  
  154.  
  155. /***  Initialization Functions  ***/
  156. #pragma mark -
  157.  
  158. /* Initialize default string for system errors */
  159. static void
  160. startupErrors ( void )
  161. {
  162.     StringHandle    tempStr;
  163.     
  164.     /* set system error default string */
  165.     
  166.     gSystemErrorStr    = (StringHandle) MemoryNewHandle ( sizeof(Str255), NULL );
  167.     if ( gSystemErrorStr == NULL )
  168.     {
  169.         return;
  170.     }
  171.     
  172.     HLockHi ( (Handle)gSystemErrorStr );
  173.     
  174.     tempStr = GetString ( krErrSystemDefault );
  175.     if ( tempStr == NULL )
  176.     {
  177.         StringPascalCopy ( (char *)ksErrSystemDefault, (char *)(*gSystemErrorStr) );
  178.     }
  179.     else
  180.     {
  181.         HLock ( (Handle)tempStr );
  182.         
  183.         StringPascalCopy ( (char *)(*tempStr), (char *)(*gSystemErrorStr) );
  184.         
  185.         HUnlock            ( (Handle)tempStr );
  186.         ReleaseResource    ( (Handle)tempStr );
  187.     }
  188.     
  189.     HUnlock ( (Handle)gSystemErrorStr );
  190. } /* startupErrors */
  191.  
  192.  
  193. /* Determine if the Gestalt manager is available */
  194. static void
  195. startupGestalt ( void )
  196. {
  197.     Boolean        gestaltAvail;
  198.     
  199.     /* determine whether the Gestalt call is available */
  200.     gestaltAvail = startupTrapAvailable ( _Gestalt );
  201.     if ( !gestaltAvail )
  202.     {
  203.         /* Gestalt require for application to work, inform the user and exit */
  204.         ErrorStartup ( kerrStartupGestalt );
  205.     }
  206. } /* startupGestalt */
  207.  
  208.  
  209. /* Determine if WaitNextEvent trap is available. */
  210. static void
  211. startupEvent ( void )
  212. {
  213.     OSErr        theErr;
  214.     SysEnvRec    theSysEnv;    /* system environment */
  215.     Boolean        WNEAvail;
  216.     
  217.     /* what are we running on here (System 4.1 or greater) */
  218.     theErr = SysEnvirons ( 1, &theSysEnv );
  219.     if ( theErr != noErr )
  220.     {
  221.         /* can't figure out what the system environment is,
  222.             inform the user and exit this application */
  223.         ErrorStartup ( kerrStartupSysEnv );
  224.     }
  225.     else
  226.     {
  227.         /* is WaitNextEvent implemented? - from Macintosh Tech Note #158 */
  228.         if ( theSysEnv.machineType > envMachUnknown )
  229.         {
  230.             WNEAvail = NGetTrapAddress(0x60, ToolTrap) != NGetTrapAddress(0x9F, ToolTrap);
  231.         }
  232.         else
  233.         {
  234.             WNEAvail = false;
  235.         }
  236.         
  237.         if ( !WNEAvail )
  238.         {
  239.             /* WaitNextEvent trap is missing - can't run */
  240.             ErrorStartup ( kerrStartupWNE );
  241.         }
  242.     }
  243. } /* startupEvent */
  244.  
  245.  
  246. /* Determine if Apple Event Manager is present and install handlers */
  247. static void
  248. startupAE ( void )
  249. {
  250.     long    feature;
  251.     OSErr    theErr;
  252.     
  253.     theErr = Gestalt ( gestaltAppleEventsAttr, &feature );
  254.     if ( (theErr != noErr) ||
  255.         !(feature & (kGestaltMask << gestaltAppleEventsPresent)) )
  256.     {
  257.         /* Apple Events not available, inform the user and exit */
  258.         ErrorStartup ( kerrStartupAppleEvent );
  259.     }
  260.  
  261.     gAEIdleUPP = NewAEIdleProc ( AEFuncAEIdleFunc );
  262.     
  263.     theErr = AEInstallHandlers ();
  264.     if ( theErr != noErr )
  265.     {
  266.         ErrorSystem ( theErr );
  267.     }
  268. } /* startupAE */
  269.  
  270.  
  271. /* Determine the space available on the main and other monitors */
  272. #if kCompileWithForeground
  273. static void
  274. startupWindows ( void )
  275. {
  276.     short         resW;
  277.     short        resH;
  278.     short        menuBarHeight;
  279.     RgnHandle    grayRgnHdl;
  280.     Rect        grayRgnBounds;
  281.     
  282.     /* get screen dimensions */
  283.     resW = qd.screenBits.bounds.right  - qd.screenBits.bounds.left;
  284.     resH = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top;
  285.     
  286.     menuBarHeight = LMGetMBarHeight ();
  287.     
  288.     /* gScreenRect is the size of the main screen, inset by a margin */
  289.     SetRect ( &gScreenRect, kScreenBorder, menuBarHeight + kScreenBorder, 
  290.         resW - kScreenBorder, resH - kScreenBorder );
  291.         
  292.     /* get entire gray rgn rect (the size of all monitors) */
  293.     grayRgnHdl        = GetGrayRgn ();            /* known as the 'gray region' */
  294.     grayRgnBounds    = (*grayRgnHdl)->rgnBBox;    /* the bounding box for the region */
  295.     
  296.     resW = grayRgnBounds.right  - grayRgnBounds.left;
  297.     resH = grayRgnBounds.bottom - grayRgnBounds.top;
  298.     
  299.     /* this rect encompasses all of the monitor desktop space,
  300.         minus the menu bar and inset by a margin. We'll use this
  301.         rectangle to limit dragging and resizing windows */
  302.     SetRect ( &gGrayRgnRect,
  303.         /* left */        kScreenBorder,
  304.         /* top */        menuBarHeight + kScreenBorder, 
  305.         /* right */        resW - kScreenBorder,
  306.         /* bottom */    resH - kScreenBorder );
  307. } /* startupWindows */
  308. #endif    /* kCompileWithForeground */
  309.  
  310.  
  311. /* QuickDraw - Graphics */
  312. #if kCompileWithForeground
  313. static void
  314. startupQuickDraw ( void )
  315. {
  316.     OSErr    theErr;
  317.     long    feature;
  318.     long    version;
  319.     
  320.     gHasColorQD = false;
  321.     
  322.     theErr = Gestalt ( gestaltQuickdrawFeatures, &feature );
  323.     if ( theErr == noErr )
  324.     {
  325.         theErr = Gestalt ( gestaltQuickdrawVersion, &version );
  326.         if ( (theErr == noErr) &&
  327.             ((feature & (kGestaltMask << gestaltHasColor)) != nil) &&
  328.             (version >= gestalt8BitQD) )
  329.         {
  330.             gHasColorQD = true;
  331.         }
  332.     }
  333. } /* startupQuickDraw */
  334. #endif    /* kCompileWithForeground */
  335.  
  336.  
  337. /* Drag Manager - interapplication drag'n'drop */
  338. #if kCompileWithDragNDrop
  339. static void
  340. startupDrag ( void )
  341. {
  342.     long    feature;
  343.     OSErr    theErr;
  344.     
  345.     theErr = Gestalt ( gestaltDragMgrAttr, &feature );
  346.     if ( (theErr == noErr) &&
  347.         (feature & (kGestaltMask << gestaltDragMgrPresent)) )
  348.     {
  349.         gHasDragNDrop = true;
  350.     }
  351.     else
  352.     {
  353.         gHasDragNDrop = false;
  354.     }
  355. } /* startupAE */
  356. #endif
  357.  
  358.  
  359. /*  */
  360. #if kCompileWithQuitOnLongIdle
  361. void
  362. startupIdleQuit ( void )
  363. {
  364.     Handle    thePref;
  365.     
  366.     /* quit after given period of idle time */
  367.     thePref = PrefItemGet ( krPrefDoIdleQuit, kPrefResType );
  368.     if ( thePref != NULL )
  369.     {
  370.         gDoIdleQuit = **((SInt16 **)thePref);
  371.         PrefItemRelease ( thePref );
  372.     }
  373.     else
  374.     {
  375.         gDoIdleQuit = true;
  376.     }
  377.     
  378.     /* no action has been performed yet */
  379.     gTimeLastAction = nil;
  380.     
  381.     /* amount of idle time to give until quit */
  382.     thePref = PrefItemGet ( krPrefIdleTimeToQuit, kPrefResType );
  383.     if ( thePref != NULL )
  384.     {
  385.         gIdleTimeToQuit    = **((UInt32 **)thePref);
  386.         PrefItemRelease ( thePref );
  387.     }
  388.     else
  389.     {
  390.         gIdleTimeToQuit    = kIdleTimeToQuit;
  391.     }
  392.     
  393.     /* if startup as normal application instead of via server event,
  394.         should we quit on idle? */
  395.     thePref = PrefItemGet ( krPrefDoIdleQuitOnOpenApp, kPrefResType );
  396.     if ( thePref != NULL )
  397.     {
  398.         gDoIdleQuitOnOpenApp = **((SInt16 **)thePref);
  399.         PrefItemRelease ( thePref );
  400.     }
  401.     else
  402.     {
  403.         gDoIdleQuitOnOpenApp = kCompileWithIdleQuitOnOpenApp;
  404.     }
  405. } /* startupIdleQuit */
  406. #endif
  407.  
  408.  
  409. #pragma mark -
  410.  
  411. /* Determine if a trap is available */
  412. static Boolean
  413. startupTrapAvailable ( unsigned long trap )
  414. {
  415.     UniversalProcPtr    theInitGrafAddr;
  416.     UniversalProcPtr    theAA6EAddr;
  417.     Boolean                trapAddrMatch;
  418.     unsigned long        numToolBoxTraps;
  419.     TrapType            theTrapType;
  420.     unsigned long        theTrapMasked;
  421.     
  422.     theInitGrafAddr    = NGetTrapAddress ( _InitGraf, ToolTrap );
  423.     theAA6EAddr        = NGetTrapAddress ( 0xAA6E, ToolTrap );
  424.     trapAddrMatch    = theInitGrafAddr == theAA6EAddr;
  425.     
  426.     if ( trapAddrMatch )
  427.     {
  428.         numToolBoxTraps = 0x200;
  429.     }
  430.     else
  431.     {
  432.         numToolBoxTraps = 0x400;
  433.     }
  434.     
  435.     theTrapType = startupTrapType ( trap );
  436.     if ( theTrapType == ToolTrap )
  437.     {
  438.         theTrapMasked = trap & 0x07FF;
  439.         if ( theTrapMasked >= numToolBoxTraps )
  440.         {
  441.             return false;
  442.         }
  443.     }
  444.     
  445.     return NGetTrapAddress ( _Unimplemented, ToolTrap ) !=
  446.             NGetTrapAddress ( trap, theTrapType );
  447. } /* startupTrapAvailable */
  448.  
  449.  
  450. /* determine the type of the trap */
  451. static TrapType
  452. startupTrapType ( unsigned long theTrap )
  453. {
  454.     /* OS traps start with A0, Tool with A8 or AA. */
  455.     if ( (theTrap & 0x0800) == nil )
  456.     {
  457.         return OSTrap;
  458.     }
  459.     else
  460.     {
  461.         return ToolTrap;
  462.     }
  463. } /* startupTrapType */
  464.  
  465.  
  466. #endif /* not kCompilingForWSAPI */
  467.  
  468. /*****  EOF  *****/
  469.